package com.adee.gulimall.cloud.alibaba.nacos;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;

import java.util.Properties;

public class NacosTest {
    private static final String serverAddr = "101.42.43.39:8848";

    private static final String namespace = "065fdc16-20df-4483-8f0f-56cb2eba7855";
    private static final String group = "DEFAULT_GROUP";
    private static final String dataId = "gulimall-common.yaml";
    private static final String content = "server.port=10086";

    public static void main(String[] args) throws NacosException {
        //test1();
        test2();
    }

    // 查询nacos配置
    public static void test1() throws NacosException {
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
        properties.put(PropertyKeyConst.NAMESPACE, namespace);
        ConfigService configService = NacosFactory.createConfigService(properties);
        //获取配置,String dataId, String group, long timeoutMs
        String content = configService.getConfig(dataId, group, 5000);
        System.out.println(content);
    }

    // 发布nacos配置
    public static void test2() {
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
        properties.put(PropertyKeyConst.NAMESPACE, namespace);
        String dataId = "test.yaml";
        try {
            //通过工厂配置服务
            ConfigService configService = NacosFactory.createConfigService(properties);
            //推送配置
            boolean isOk = configService.publishConfig(dataId, group, content);
            System.out.println("是否成功：" + isOk);
        } catch (NacosException e) {
            e.printStackTrace();
        }
    }
}
